package logic

import (
	"context"
	"fmt"
	"shopping-mall/models"

	"shopping-mall/shopping_rpc/internal/svc"
	"shopping-mall/shopping_rpc/shopping_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProductLogic {
	return &AddProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddProductLogic) AddProduct(in *shopping_rpc.AddProductRequest) (*shopping_rpc.AddProductResponse, error) {
	// todo: add your logic here and delete this line
	product := models.Product{
		UserId:       int(in.UserId),
		Title:        in.Title,
		Price:        float64(in.Price),
		BrowseNumber: 0,
		Image:        in.Image,
		ClassId:      int(in.ClassId),
	}
	if models.DB.Create(&product).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &shopping_rpc.AddProductResponse{}, nil
}
